package com.adxon.servlet;

import com.adxon.MsgException;
import com.adxon.Uri;
import com.adxon.cookie.Station;
import com.adxon.data.Following;
import com.adxon.data.Item;
import com.adxon.data.ItemSet;
import com.adxon.data.Link;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public class HomeServlet extends ServletBase{
	public void doGet(HttpServletRequest req,HttpServletResponse resp)
			throws IOException,ServletException{
		try{
			System.out.println("HomeServlet: "+req.getRequestURI());
			Item u=Station.getCurrentUser(req);
			if(req.getRequestURI().length()<2){
				setAttributes(req,null);
				if(u==null)
					forward("guest.jsp",req,resp);
				else{
					req.setAttribute("b","all");
					Item s=new Item();
					s.setRate(9);
					s.setOwner(u);
					s.setStarer(u);
					req.setAttribute("m",new Following(s));
					forward("following.jsp",req,resp);
				}
			}else{
				Uri n=new Uri(req);
				System.out.println("key="+n+", path="+n.getPath()+", id="+
						n.getId()+", type="+n.getType()+", ext="+
						n.getExtension()+", tags="+n.getTags());//*/
				String g=n.getTags();
				Item i=((Item)new Item().update(n.toString())).extend();
				i.setStarer(u);
				setAttributes(req,i.getText());
				Item s=new Link();
				s.setKind();
				s.setOwner(i.getId());
				s.setRate(19);
				s.setStarer(u);
				s.setTags(g);
				req.setAttribute("i",i);
				req.setAttribute("b",n);
				req.setAttribute("g",g);
				req.setAttribute("m",new ItemSet(s));
				forward("personal.jsp",req,resp);
			}
		}catch(MsgException e){
			e.routing(req,resp);
		}
	}
}